package com.sunbeam.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 
 * @ClassName: InvokeUtils
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/08/31 16:53:36
 *
 */
public class InvokeUtils {

	//************************TestMain**************************************
	/**
	 * 
	 * @Title: main
	 * @Description: TODO
	 * @param @param args
	 * @return void
	 * @throws
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		UserInfoEntity userInfo = new UserInfoEntity();
//		ArrayList<UserInfoEntity> userInfos = new ArrayList<UserInfoEntity>();
//
//		Field[] fields = getFields(userInfo);
//		Method[] methods = getMethods(userInfo);
//
//		for (int i = 0; i < 100; i++) {
//			userInfo = new UserInfoEntity();
//			for (int j = 0; j < fields.length; j++) {
//				for (int k = 0; k < methods.length; k++) {
//					Object[] obj = new Object[1];
//					if (methods[k].getName()
//							.equals("set" + fields[j].getName())) {
//						if (fields[j].getType().equals(String.class)) {
//							obj[0] = fields[j].getName() + StringUtils.getStringSequence(i + 1, 2);
//							execute(userInfo, methods[k].getName(), obj);
//						} else {
//							obj[0] = new Date(System.currentTimeMillis()
//									- (long) (Math.random() * 999999999999L));
//							execute(userInfo, methods[k].getName(), obj);
//						}
//					}
//				}
//			}
//			userInfos.add(userInfo);
//		}
//		for (UserInfoEntity userInfoObj : userInfos) {
//			System.out.println(userInfoObj.getUID());
//		}
	}
	//************************TestMain**************************************
	
	/**
	 * 
	 * @Title: getMethods
	 * @Description: TODO
	 * @param @param object
	 * @param @return
	 * @return Method[]
	 * @throws
	 */
	public static Method[] getMethods(Object object) {
		Class<? extends Object> clazz = object.getClass();
		Method[] methods = clazz.getDeclaredMethods();
		return methods;
	}

	
	/**
	 * 
	 * @Title: getFields
	 * @Description: TODO
	 * @param @param object
	 * @param @return
	 * @return Field[]
	 * @throws
	 */
	public static Field[] getFields(Object object) {
		Class<? extends Object> clazz = object.getClass();
		Field[] fields = clazz.getDeclaredFields();
		return fields;
	}

	
	/**
	 * @param <T>
	 * 
	 * @Title: execute
	 * @Description: TODO
	 * @param @param object
	 * @param @param methodName
	 * @param @param parameters
	 * @return void
	 * @throws
	 */
	public static Object execute(Object object, String methodName,
			Object[] parameters) {
		Class<? extends Object> Clazz = object.getClass();
		Method[] methods = Clazz.getMethods();
		for (Method method : methods) {
			if (method.getName().equals(methodName)) {
					if(!method.isAccessible()){
						method.setAccessible(true);
					}
					try {
						return method.invoke(object, parameters);
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
		}
		return null;
	}
}
